package com.speed.controller.base;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

public class BaseController extends Controller {

	public void index() {
		// Cache cache = Redis.use("user");
		//
		// cache.set("ui", 200);
		//
		// // 获取Jedis 这步骤是用与保存数据到硬盘上，因为是一次性保存的，保存操作之前，必须思考哪些数据是没必要保留缓存的
		// cache.getJedis().save();
		render("index.html");
	}

	public void test() {

		/**
		 * 这里可以使用 Redis 缓存起来登陆信息 ，以下代码是示例，不是正式代码，仅供参考
		 */

		Cache user = Redis.use("user");

		String username = user.get("username");

		String pw = user.get("password");

		UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("user", "password", true);

		Subject subject = SecurityUtils.getSubject();

		// 这个session 不是 http session 是 apache shiro 的 session
		// 默认的情况下 这个session 可以是 http session
		Session session = subject.getSession();

		session.setAttribute("user", "aaaaaa");

		try {
			subject.login(usernamePasswordToken);
			// if no exception, that's it, we're done!
		} catch (UnknownAccountException uae) {
			// username wasn't in the system, show them an error message?
		} catch (IncorrectCredentialsException ice) {
			// password didn't match, try again?
		} catch (LockedAccountException lae) {
			// account for that username is locked - can't login. Show them a
			// message?
		} catch (AuthenticationException ae) {
			// unexpected condition - error?
		}

		// Cache cache = Redis.use("user");

		// renderText((String) (cache.get("ui") + "222"));
	}
}